package com.icity.util;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.ocean.rawsdk.ApiExecutor;
import com.alibaba.ocean.rawsdk.client.exception.OceanException;
import com.icity.entity.ActiveUserInfo;
import com.icity.entity.LaunchInfo;
import com.icity.entity.NewUserInfo;
import com.icity.mapper.UmStatisticMapper;
import com.icity.service.UmApiService;
import com.umeng.uapp.param.UmengUappGetActiveUsersParam;
import com.umeng.uapp.param.UmengUappGetLaunchesParam;
import com.umeng.uapp.param.UmengUappGetLaunchesResult;
import com.umeng.uapp.param.UmengUappGetNewUsersParam;
import com.umeng.uapp.param.UmengUappGetNewUsersResult;
import com.umeng.uapp.param.UmengUappGetActiveUsersResult;
import com.umeng.uapp.param.UmengUappGetChannelDataParam;
import com.umeng.uapp.param.UmengUappGetDurationsParam;
import com.umeng.uapp.param.UmengUappGetDurationsResult;
import com.umeng.uapp.param.UmengUappGetChannelDataResult;
@Component

public class TestOpenAPI {

	@Autowired
	private UmStatisticMapper umStatisticMapper;

	// 每日的启动次数
	public String umengUappGetLaunches(ApiExecutor apiExecutor, String date) {
		UmengUappGetLaunchesParam param = new UmengUappGetLaunchesParam();
		// 测试环境只支持http
		// param.getOceanRequestPolicy().setUseHttps(false);
		param.setAppkey("5b7a29f1f43e482a4a0000b5");
		param.setStartDate(date);
		param.setEndDate(date);
		param.setPeriodType("daily");

		try {
			UmengUappGetLaunchesResult result = apiExecutor.execute(param);
			return JSONObject.toJSONString(result);
		} catch (OceanException e) {
			return "errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage();
		}
	}

	// 每日新增用户数
	public String umengUappGetNewUsers(ApiExecutor apiExecutor, String date) {
		UmengUappGetNewUsersParam param = new UmengUappGetNewUsersParam();
		// 测试环境只支持http
		// param.getOceanRequestPolicy().setUseHttps(false);
		param.setAppkey("5b7a29f1f43e482a4a0000b5");
		param.setStartDate(date);
		param.setEndDate(date);
		param.setPeriodType("daily");

		try {
			UmengUappGetNewUsersResult result = apiExecutor.execute(param);
			return JSONObject.toJSONString(result);
		} catch (OceanException e) {
			return "errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage();
		}
	}

	// 每日日活数
	public String umengUappGetActiveUsers(ApiExecutor apiExecutor, String date) {
		UmengUappGetActiveUsersParam param = new UmengUappGetActiveUsersParam();
		// 测试环境只支持http
		// param.getOceanRequestPolicy().setUseHttps(false);
		param.setAppkey("5b7a29f1f43e482a4a0000b5");
		param.setStartDate(date);
		param.setEndDate(date);
		param.setPeriodType("daily");

		try {
			UmengUappGetActiveUsersResult result = apiExecutor.execute(param);
			return JSONObject.toJSONString(result);
		} catch (OceanException e) {
			return "errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage();
		}
	}

	// 获取每日使用时间
	public String umengUappGetDurations(ApiExecutor apiExecutor, String date) {
		UmengUappGetDurationsParam param = new UmengUappGetDurationsParam();
		// 测试环境只支持http
		// param.getOceanRequestPolicy().setUseHttps(false);
		param.setAppkey("5b7a29f1f43e482a4a0000b5");
		param.setDate(date);
		param.setStatType("daily");
		// param.setChannel("App%20Store");
		// param.setVersion("1.0.0");

		try {
			UmengUappGetDurationsResult result = apiExecutor.execute(param);
			return JSONObject.toJSONString(result);
		} catch (OceanException e) {
			return "errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage();
		}
	}

	// 查询每日各渠道的用户数
	public String umengUappGetChannelData(ApiExecutor apiExecutor) {
		UmengUappGetChannelDataParam param = new UmengUappGetChannelDataParam();
		// 测试环境只支持http
		// param.getOceanRequestPolicy().setUseHttps(false);
		param.setAppkey("5b7a29f1f43e482a4a0000b5");
		param.setDate("2018-12-01");

		try {
			UmengUappGetChannelDataResult result = apiExecutor.execute(param);
			return JSONObject.toJSONString(result);
		} catch (OceanException e) {
			return "errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage();
		}
	}

	public void save(String jsonStr){
		JSONObject json = JSONObject.parseObject(jsonStr);
		if(json.containsKey("launchInfo")){
			LaunchInfo launchInfo = new LaunchInfo();
			JSONArray jsonArray = json.getJSONArray("launchInfo");
			JSONObject jsonObject = jsonArray.getJSONObject(0);
			launchInfo.setDate(jsonObject.getString("date"));
			launchInfo.setValue(jsonObject.getInteger("value"));
			System.out.println(launchInfo.getValue());
			umStatisticMapper.insertLaunchinfo(launchInfo);
		}else if(json.containsKey("newUserInfo")){
			NewUserInfo newUserInfo = new NewUserInfo();
			JSONArray jsonArray = json.getJSONArray("newUserInfo");
			JSONObject jsonObject = jsonArray.getJSONObject(0);
			newUserInfo.setDate(jsonObject.getString("date"));
			newUserInfo.setValue(jsonObject.getInteger("value"));
			umStatisticMapper.insertNewUserInfo(newUserInfo);
		}else if(json.containsKey("activeUserInfo")){
			ActiveUserInfo activeUserInfo = new ActiveUserInfo();
			JSONArray jsonArray = json.getJSONArray("activeUserInfo");
			JSONObject jsonObject = jsonArray.getJSONObject(0);
			activeUserInfo.setDate(jsonObject.getString("date"));
			activeUserInfo.setValue(jsonObject.getInteger("value"));
			umStatisticMapper.insertActiveUserInfo(activeUserInfo);
		}
		System.out.println("success");
	}

	public static void getUm(){
		// 请替换apiKey和apiSecurity
		ApiExecutor apiExecutor = new ApiExecutor("5764644", "7xCp4NJ1mV6");
		apiExecutor.setServerHost("gateway.open.umeng.com");
		TestOpenAPI testOpenAPI = new TestOpenAPI();
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 1; i <= 2; i++) {
			String dateStr = DateUtils.getSpecifiedDayBefore(sdf.format(date), i);
			String launchInfo = testOpenAPI.umengUappGetLaunches(apiExecutor, dateStr);
			String newUserInfo = testOpenAPI.umengUappGetNewUsers(apiExecutor, dateStr);
			String activeUserInfo = testOpenAPI.umengUappGetActiveUsers(apiExecutor, dateStr);
			//String durationInfo = testOpenAPI.umengUappGetDurations(apiExecutor, dateStr);
			testOpenAPI.save(launchInfo);
			testOpenAPI.save(newUserInfo);
			testOpenAPI.save(activeUserInfo);
			//testOpenAPI.save(durationInfo);
		}
	}

//	public static void main(String[] args) {
//		ApiExecutor apiExecutor = new ApiExecutor("5764644", "7xCp4NJ1mV6");
//		apiExecutor.setServerHost("gateway.open.umeng.com");
//		TestOpenAPI testOpenAPI = new TestOpenAPI();
//		System.out.println(testOpenAPI.umengUappGetChannelData(apiExecutor));
//	}


}
